const CompanyService = require('../../service/company')
const Response = require('../../utils/response')

async function newCompany(ctx) {
    ctx.request.body.status = 0
    const response = new Response()
    const result = await CompanyService.newCompany({
        ctx
    })
    if (result.result) {
        response.SUCCESS = 200
        response.DATA = result
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

async function getAllCompany(ctx) {
    const result = await CompanyService.getAllCompany({ctx})
    const response = new Response()
    response.SUCCESS = 200
    response.DATA = {
        count: result.length,
        Companys: result
    }
    ctx.body = response.getData()
}

async function getCompanyById(ctx) {
    const { id } = ctx.params
    const { company } = await CompanyService.getCompanyById({ ctx, id })
    const response = new Response()

    if(company) {
      response.SUCCESS = 200
      response.DATA = company
    }else{
      response.FAIL = 500
      response.DATA = '记录不存在'
    }
    ctx.body = response.getData()
}


async function delCompany(ctx) {
    const {
        id
    } = ctx.request.body

    const result = await CompanyService.delCompany({
        id
    })

    const response = new Response()

    if (result) {
        response.SUCCESS = 200
        response.DATA = '删除成功'
    } else {
        response.FAIL = 500
        response.DATA = '删除错误'
    }
    ctx.body = response.getData()
}

async function setCompany(ctx) {
    const {
        id
    } = ctx.request.body
    const result = await CompanyService.setCompany({
        ctx,
        id
    })
    const response = new Response()
    if (result) {
        response.SUCCESS = 200
        response.DATA = '修改成功'
    } else {
        response.FAIL = 500
        response.DATA = '错误'
    }
    ctx.body = response.getData()
}

module.exports = {
    newCompany,
    getAllCompany,
    delCompany,
    setCompany,
    getCompanyById
}